#include "mainwindow.h"

#include <QApplication>
#include <QString>
#include <QDir>

#include <QtWebSockets/QWebSocket>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);

    QWebSocket socket;
       socket.open(QUrl("ws://localhost:1234")); // WebSocket服务器的URL

       QString savePath = QDir::homePath() + "/" + "save.txt";// 要保存接收文件的路径
       QFile file(savePath);
       if (file.open(QIODevice::WriteOnly)) {
           qDebug() << "open file successfully.";
           QObject::connect(&socket, &QWebSocket::binaryMessageReceived, [&](const QByteArray& message) {
               qDebug() << "writing.";
               file.write(message); // 将接收到的数据写入文件
           });
       } else {
           qDebug() << "Failed to open file for writing.";
       }

       // 等待文件接收完成
       QObject::connect(&socket, &QWebSocket::disconnected, [&]() {
           file.close();
           socket.close();
           qDebug() << "finish receiving and close files.";
           //QCoreApplication::quit();
       });

    MainWindow w;
    w.show();
    return a.exec();
}
